<?php
Class module_autodj extends module {
	
	public $debug = null;
	
	function getSimilar() {
		$q = request::get("q");
		
		$this->debug = null; 
			
		if (!empty($q)) {
			request::set("recent_similar", array(), "session");
			$this->debug = "0: Initialize session, looking up new artist $q";
		} else {
			if (rand(1, config::getInstance()->autodj_random_interval) != 1) {
				// lookup last artist
				$q = $this->getLastArtistFromSession();
				$this->debug = "1: Continue session, looking up last artist $q";
			} else {
				// show random video from session
				$this->debug = "2: Continue session, random from session";
				return $this->outputRandomVideoFromSession();
			}
		}
				
		// get artists
		$similar = lastfm::getSimilarArtists(substr($q, 0, 30));
		
		// limit by artist range
		if (count($similar) > config::getInstance()->autodj_limit_artists) {
			array_splice($similar, config::getInstance()->autodj_limit_artists); 
		}
		
		// get random one
		$artist = $similar[rand(0, count($similar) - 1)];
		
		// no artist found, output video from session
		if (empty($artist)) {
			$this->debug = "3: No similar artist found, random from session";
			return $this->outputRandomVideoFromSession();
		}
		
		// get 20 youtube videos for artist
		$videos = youtube::getVideos($artist, 1, 20);

		// set artist of videos to lastfm result
		foreach($videos as $video) {
			/* @var $video video */
			$video->getMetaDataObject()->set("artist", $artist);
			$video->getMetaDataObject()->set("title", $video->getMetaDataObject()->getTitle(true));
		}
		
		$key = $videos->randomKeys(1);
		/* @var $video video */
		$video = ($res = $videos->get($key)) ? $res : array();
		
		// no video found, output video from session
		if (empty($video)) {
			$this->debug = "4: No video found for similar artist $artist, random from session";
			return $this->outputRandomVideoFromSession();
		}
		
		$this->storeToSession($videos);
		$this->output($video->getAll(true, false));
	}
	
	function output($video, $template = null) {
		$this->attach("video", $video);
		$this->attach("debug", $this->debug);				
		parent::output($template);
	}
	
	function getLastArtistFromSession() {
		/* @var $recent video_collection */
		$recent = request::get("recent_similar", "session");
		if ($recent) {
			/* @var $first video */
			$first = $recent->getFirst();
			$artist = $first->getMetaDataObject()->getArtist(false);
			return $artist; 
		} else {
			return false;
		}		
	}
	
	function outputRandomVideoFromSession() {
		/* @var $videos video_collection */
		$videos = request::get("recent_similar", "session");
		if ($videos) {
			$video = $videos->get($videos->randomKeys(1));
		}
		if ($video) {
			$this->output($video->getAll(true, false));
		} else {
			$this->debug = "5: No video found for similar artist $artist, no videos in session";
			$this->output(array());
		}
	}
	
	function storeToSession($videos) {
		/* @var $recentVideos video_collection */
		$recentVideos = request::get("recent_similar", "session");
		if (!is_object($recentVideos)) {
			$recentVideos = new video_collection();
		}
		$recentVideos->limitElements(-80);
		$recentVideos->mergeItems($videos);
		request::set("recent_similar", $recentVideos, "session");
	}
	
}
?>